package com.why.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;

/**
 * @author new wei
 * @date 2022/5/15 11:33
 */
public class GiteeUtil {
    public static Object giteeUtil(String code,String redirect_url){
        String url="https://gitee.com/oauth/token?grant_type=authorization_code";
                /*"code={code}&" +
                "client_id={client_id}" +
                "redirect_uri={redirect_uri}&" +
                "client_secret={client_secret}"*/
        //POST请求
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", code);
        paramMap.put("client_id", "dd05f42c61ee4e695b1d54c77437ea15cbe7c1b024d758036147a64cd4218415");
        paramMap.put("redirect_uri", redirect_url);
        paramMap.put("client_secret", "057d79cb632a5f734a93d3cfc89609caf55a0cfcb4f86cbfad9d0258062a8a1c");

        String token = HttpUtil.post(url, paramMap);
        JSONObject jsonObject = JSON.parseObject(token);
        Object access_token = jsonObject.get("access_token");
        String url2="https://gitee.com/api/v5/user?access_token="+access_token;
        JSONObject jsonObject1 = JSON.parseObject(HttpUtil.get(url2));
        Object name = jsonObject1.get("name");
        return name;
    }
}
